﻿/*----------------------------------------------------------------
       // Copyright (C) 2008 安华顺通(北京)科技有限公司
       // 版权所有。 
       //
       // 文件名：ContextMenu.cs
       // 文件功能描述：右键菜单的提供类，利用单态设计模式，提供所有右键菜单；
       //
       // 
       // 创建标识：杨波
       //
       // 修改标识：杨波20080828
       // 修改描述：添加TableFieldContextMenu,MenuContextMenu,MenuItemContextMenu
       //
       // 修改标识：杨波20080925
       // 修改描述：添加TableRelationContextMenu
       //
       // 修改标识：杨波20080926
       // 修改描述：添加TableDeleteActionContextMenu
       //
       // 修改标识：
       // 修改描述：
//----------------------------------------------------------------*/

#region UsingStatements

using System.Collections;
using System.Windows.Forms;

#endregion

namespace RuleCode.ObjectTree
{
    internal class SysContextMenu
    {
        //利用单态设计模式，提供所有右键菜单；        
        private static TablesContextMenu tables;
        private static BaseContextMenu ret;
        private static ExtendDataTypesContextMenu edt;
        private static BaseContextMenu baseContext;
        private static ExtendDataTypeContextMenu extendDataType;
        private static TableContextMenu table;
        private static TableFieldContextMenu tableField;
        private static MenuItemGroupContextMenu menuItemGroup;
        private static MenusContextMenu menus;
        private static MenuItemContextMenu menuItem;
        private static MenuContextMenu menu;
        private static MenuItemInMenuContextMenu menuItemInMenu;
        private static MenuItemsContextMenu menuItems;
        private static ExtendDataTypeRelationsContextMenu edtRelations;
        private static ExtendDataTypeRelationContextMenu edtRelation;
        private static BaseEnumsContextMenu baseEnums;
        private static BaseEnumContextMenu baseEnum;
        private static BaseEnumElementContextMenu baseEnumElement;
        private static TableFieldGroupsContextMenu fieldGroups;
        private static TableFieldGroupContextMenu fieldGroup;
        private static TableIndexesContextMenu indexes;
        private static TableIndexContextMenu index;
        private static TableFieldGroupFieldContextMenu fieldGroupField;
        private static TableIndexFieldContextMenu indexField;
        private static TableRelationsContextMenu tableRelations;
        private static TableRelationContextMenu tableRelation;
        private static TableRelationFieldContextMenu tableRelationField;
        private static TableDeleteActionsContextMenu tableDeleteActions;
        private static TableDeleteActionContextMenu tableDeleteAction;
        private static TableFieldsContextMenu tableFields;
        private static TableMethodsContextMenu tableMethods;
        private static TableMethodContextMenu tableMethod;
        private static FormsContextMenu forms;
        private static FormContextMenu form;
        private static QueriesContextMenu queries;
        private static QueryContextMenu query;
        private static QueryMethodsContextMenu queryMotheds;
        private static QueryMethodContextMenu queryMethod;
        private static QueryDataSourcesContextMenu queryDataSources;
        private static QueryDataSourceContextMenu queryDataSource;
        private static QueryDataSourceSortingsContextMenu queryDataSourceSortings;
        private static QueryDataSourceSortingContextMenu queryDataSourceSorting;
        private static QueryDataSourceRangesContextMenu queryDataSourceRanges;
        private static QueryDataSourceRangeContextMenu queryDataSourceRange;
        private static FormDesignContextMenu formDesignContextMenu;
        private static ClassesContextMenu classesContextMenu;
        private static ClassContextMenu classContextMenu;
        private static FormsDataSourcesContextMenu formsDataSourcesContextMenu;
        private static WinControlContextMenu winControlContextMenu;
        private static TabContextMenu tabContextMenu;
        private static DeleteContextMenu deleteContextMenu;
        private static WebFormsContextMenu webFormsContextMenu;
        private static WebFormSinglesContextMenu webFormSinglsContextMenu;
        private static WebFormMasterSlavesContextMenu webFormMasterSlavesContextMenu;
        private static WebFormTreesContextMenu webFormTreesContextMenu;
        private static WebFormParametersContextMenu webFormParametersContextMenu;
        private static WebFormParaByUsersContextMenu webFormParaByUsersContextMenu;
        private static WebFormRelationsContextMenu webFormRelationsContextMenu;
        private static WebFormSingleContextMenu webFormSingleContextmenu;
        private static WebFormMasterSlaveContextMenu webFormMasterSlaveContextMenu;
        private static WebFormTreeContextMenu webFormTreeContextMenu;
        private static WebFormParameterContextMenu webFormParameterContextMenu;
        private static WebFormParaByUserContextMenu webFormParaByUserContextMenu;
        private static WebFormRelationContextMenu webFormRelationContextMenu;
        private static WebFormFieldGroupsContextMenu webFormFieldGroupsContextMenu;
        private static WebFormButtonsContextMenu webFormButtonsContextMenu;
        private static WebFormButtonContextMenu webFormButtonContextMenu;
        private static WebFormFieldGroupContextMenu webFormFieldGroupContextMenu;
        private static WebFormBlankContextMenu webFormBlankContextMenu;
        private static WebFormBlanksContextMenu webFormBlanksContextMenu;

        public static ContextMenuStrip getContextMenu(string nodeType)
        {
            switch (nodeType)
            {
                case OT.Tables:
                    {
                        /*if (tables == null)
                        {
                            tables = new TablesContextMenu();
                            ret = tables;
                        }
                        else
                        {
                            ret = tables;
                        }*/
                        tables = tables ?? new TablesContextMenu();
                        ret = tables;
                        break;
                        //添加其它的右键菜单
                    }
                case OT.ExtendDataTypes:
                    {
                        if (edt == null)
                        {
                            edt = new ExtendDataTypesContextMenu();
                            ret = edt;
                        }
                        else
                        {
                            ret = edt;
                        }
                        break;
                    }
                case OT.ExtendDataType:
                    {
                        if (extendDataType == null)
                        {
                            extendDataType = new ExtendDataTypeContextMenu();
                            ret = extendDataType;
                        }
                        else
                        {
                            ret = extendDataType;
                        }
                        break;
                    }
                case OT.Table:
                    {
                        if (table == null)
                        {
                            table = new TableContextMenu();
                            ret = table;
                        }
                        else
                        {
                            ret = table;
                        }
                        break;
                    }
                case OT.TableField:
                    {
                        if (tableField == null)
                        {
                            tableField = new TableFieldContextMenu();
                            ret = tableField;
                        }
                        else
                        {
                            ret = tableField;
                        }
                        break;
                    }
                case OT.MenuItemGroup:
                    {
                        if (menuItemGroup == null)
                        {
                            menuItemGroup = new MenuItemGroupContextMenu();
                            ret = menuItemGroup;
                        }
                        else
                        {
                            ret = menuItemGroup;
                        }
                        break;
                    }
                case OT.Menus:
                    {
                        if (menus == null)
                        {
                            menus = new MenusContextMenu();
                            ret = menus;
                        }
                        else
                        {
                            ret = menus;
                        }
                        break;
                    }
                case OT.Menu:
                    {
                        if (menu == null)
                        {
                            menu = new MenuContextMenu();
                            ret = menu;
                        }
                        else
                        {
                            ret = menu;
                        }
                        break;
                    }
                case OT.MenuItem:
                    if (menuItem == null)
                    {
                        menuItem = new MenuItemContextMenu();
                        ret = menuItem;
                    }
                    else
                    {
                        ret = menuItem;
                    }
                    break;
                case OT.MenuItemInMenu:
                    {
                        if (menuItemInMenu == null)
                        {
                            menuItemInMenu = new MenuItemInMenuContextMenu();
                            ret = menuItemInMenu;
                        }
                        else
                        {
                            ret = menuItemInMenu;
                        }
                        break;
                    }
                case OT.MenuItems:
                    {
                        if (menuItems == null)
                        {
                            menuItems = new MenuItemsContextMenu();
                            ret = menuItems;
                        }
                        else
                        {
                            ret = menuItems;
                        }
                        break;
                    }
                case OT.EDTRelations:
                    {
                        if (edtRelations == null)
                        {
                            edtRelations = new ExtendDataTypeRelationsContextMenu();
                            ret = edtRelations;
                        }
                        else
                        {
                            ret = edtRelations;
                        }
                        break;
                    }
                case OT.EDTRelation:
                    {
                        if (edtRelation == null)
                        {
                            edtRelation = new ExtendDataTypeRelationContextMenu();
                            ret = edtRelation;
                        }
                        else
                        {
                            ret = edtRelation;
                        }
                        break;
                    }
                case OT.BaseEnums:
                    {
                        if (baseEnums == null)
                        {
                            baseEnums = new BaseEnumsContextMenu();
                            ret = baseEnums;
                        }
                        else
                        {
                            ret = baseEnums;
                        }
                        break;
                    }
                case OT.BaseEnum:
                    {
                        if (baseEnum == null)
                        {
                            baseEnum = new BaseEnumContextMenu();
                            ret = baseEnum;
                        }
                        else
                        {
                            ret = baseEnum;
                        }
                        break;
                    }
                case OT.EnumElement:
                    {
                        if (baseEnumElement == null)
                        {
                            baseEnumElement = new BaseEnumElementContextMenu();
                            ret = baseEnumElement;
                        }
                        else
                        {
                            ret = baseEnumElement;
                        }
                        break;
                    }
                case OT.TableFieldGroups:
                    {
                        if (fieldGroups == null)
                        {
                            fieldGroups = new TableFieldGroupsContextMenu();
                            ret = fieldGroups;
                        }
                        else
                        {
                            ret = fieldGroups;
                        }
                        break;
                    }
                case OT.FieldGroup:
                    {
                        if (fieldGroup == null)
                        {
                            fieldGroup = new TableFieldGroupContextMenu();
                            ret = fieldGroup;
                        }
                        else
                        {
                            ret = fieldGroup;
                        }
                        break;
                    }
                case OT.TableIndexes:
                    {
                        if (indexes == null)
                        {
                            indexes = new TableIndexesContextMenu();
                            ret = indexes;
                        }
                        else
                        {
                            ret = indexes;
                        }
                        break;
                    }
                case OT.Index:
                    {
                        if (index == null)
                        {
                            index = new TableIndexContextMenu();
                            ret = index;
                        }
                        else
                        {
                            ret = index;
                        }
                        break;
                    }
                case OT.GroupField:
                    {
                        if (fieldGroupField == null)
                        {
                            fieldGroupField = new TableFieldGroupFieldContextMenu();
                            ret = fieldGroupField;
                        }
                        else
                        {
                            ret = fieldGroupField;
                        }
                        break;
                    }
                case OT.IndexField:
                    {
                        if (indexField == null)
                        {
                            indexField = new TableIndexFieldContextMenu();
                            ret = indexField;
                        }
                        else
                        {
                            ret = indexField;
                        }
                        break;
                    }
                case OT.TableRelations:
                    {
                        if (tableRelations == null)
                        {
                            tableRelations = new TableRelationsContextMenu();
                            ret = tableRelations;
                        }
                        else
                        {
                            ret = tableRelations;
                        }
                        break;
                    }
                case OT.TableRelation:
                    {
                        if (tableRelation == null)
                        {
                            tableRelation = new TableRelationContextMenu();
                            ret = tableRelation;
                        }
                        else
                        {
                            ret = tableRelation;
                        }
                        break;
                    }
                case OT.TableRelationField:
                    {
                        if (tableRelationField == null)
                        {
                            tableRelationField = new TableRelationFieldContextMenu();
                            ret = tableRelationField;
                        }
                        else
                        {
                            ret = tableRelationField;
                        }
                        break;
                    }
                case OT.TableDeleteActions:
                    {
                        if (tableDeleteActions == null)
                        {
                            tableDeleteActions = new TableDeleteActionsContextMenu();
                            ret = tableDeleteActions;
                        }
                        else
                        {
                            ret = tableDeleteActions;
                        }
                        break;
                    }
                case OT.TableDeleteAction:
                    {
                        if (tableDeleteAction == null)
                        {
                            tableDeleteAction = new TableDeleteActionContextMenu();
                            ret = tableDeleteAction;
                        }
                        else
                        {
                            ret = tableDeleteAction;
                        }
                        break;
                    }
                case OT.TableFields:
                    {
                        if (tableFields == null)
                        {
                            tableFields = new TableFieldsContextMenu();
                            ret = tableFields;
                        }
                        else
                        {
                            ret = tableFields;
                        }
                        break;
                    }
                case OT.TableMethods:
                    {
                        if (tableMethods == null)
                        {
                            tableMethods = new TableMethodsContextMenu();
                            ret = tableMethods;
                        }
                        else
                        {
                            ret = tableMethods;
                        }
                        break;
                    }
                case OT.TableMethod:
                    {
                        if (tableMethod == null)
                        {
                            tableMethod = new TableMethodContextMenu();
                            ret = tableMethod;
                        }
                        else
                        {
                            ret = tableMethod;
                        }
                        break;
                    }
                case OT.Forms:
                    {
                        if (forms == null)
                        {
                            forms = new FormsContextMenu();
                            ret = forms;
                        }
                        else
                        {
                            ret = forms;
                        }
                        break;
                    }
                case OT.Form:
                    {
                        if (form == null)
                        {
                            form = new FormContextMenu();
                            ret = form;
                        }
                        else
                        {
                            ret = form;
                        }
                        break;
                    }
                case OT.Queries:
                    {
                        if (queries == null)
                        {
                            queries = new QueriesContextMenu();
                            ret = queries;
                        }
                        else
                        {
                            ret = queries;
                        }
                        break;
                    }
                case OT.Query:
                    {
                        if (query == null)
                        {
                            query = new QueryContextMenu();
                            ret = query;
                        }
                        else
                        {
                            ret = query;
                        }
                        break;
                    }
                case OT.QueryMethods:
                    {
                        if (queryMotheds == null)
                        {
                            queryMotheds = new QueryMethodsContextMenu();
                            ret = queryMotheds;
                        }
                        else
                        {
                            ret = queryMotheds;
                        }
                        break;
                    }
                case OT.QueryMethod:
                    {
                        if (queryMethod == null)
                        {
                            queryMethod = new QueryMethodContextMenu();
                            ret = queryMethod;
                        }
                        else
                        {
                            ret = queryMethod;
                        }
                        break;
                    }
                case OT.QueryDataSources:
                    {
                        if (queryDataSources == null)
                        {
                            queryDataSources = new QueryDataSourcesContextMenu();
                            ret = queryDataSources;
                        }
                        else
                        {
                            ret = queryDataSources;
                        }
                        break;
                    }
                case OT.QueryDataSource:
                    {
                        if (queryDataSource == null)
                        {
                            queryDataSource = new QueryDataSourceContextMenu();
                            ret = queryDataSource;
                        }
                        else
                        {
                            ret = queryDataSource;
                        }
                        break;
                    }
                case OT.QueryDataSourceSortings:
                    {
                        if (queryDataSourceSortings == null)
                        {
                            queryDataSourceSortings = new QueryDataSourceSortingsContextMenu();
                            ret = queryDataSourceSortings;
                        }
                        else
                        {
                            ret = queryDataSourceSortings;
                        }
                        break;
                    }
                case OT.QueryDataSourceSorting:
                    {
                        if (queryDataSourceSorting == null)
                        {
                            queryDataSourceSorting = new QueryDataSourceSortingContextMenu();
                            ret = queryDataSourceSorting;
                        }
                        else
                        {
                            ret = queryDataSourceSorting;
                        }
                        break;
                    }
                case OT.QueryDataSourceRanges:
                    {
                        if (queryDataSourceRanges == null)
                        {
                            queryDataSourceRanges = new QueryDataSourceRangesContextMenu();
                            ret = queryDataSourceRanges;
                        }
                        else
                        {
                            ret = queryDataSourceRanges;
                        }
                        break;
                    }
                case OT.QueryDataSourceRange:
                    {
                        if (queryDataSourceRange == null)
                        {
                            queryDataSourceRange = new QueryDataSourceRangeContextMenu();
                            ret = queryDataSourceRange;
                        }
                        else
                        {
                            ret = queryDataSourceRange;
                        }
                        break;
                    }
                case OT.Panel:
                    {
                        formDesignContextMenu = formDesignContextMenu ?? new FormDesignContextMenu();
                        ret = formDesignContextMenu;
                        break;
                    }
                case OT.Classes:
                    {
                        classesContextMenu = classesContextMenu ?? new ClassesContextMenu();
                        ret = classesContextMenu;
                        break;
                    }
                case OT.Class:
                    {
                        classContextMenu = classContextMenu ?? new ClassContextMenu();
                        ret = classContextMenu;
                        break;
                    }
                case OT.FormDataSources:
                    {
                        formsDataSourcesContextMenu = formsDataSourcesContextMenu ?? new FormsDataSourcesContextMenu();
                        ret = formsDataSourcesContextMenu;
                        break;
                    }
                //Todo:将所有WinControl类型的结点绑定右键菜单。
                case OT.TabPage:
                    {
                        winControlContextMenu = winControlContextMenu ?? new WinControlContextMenu();
                        ret = winControlContextMenu;
                        break;
                    }
                case OT.Grid:
                    {
                        winControlContextMenu = winControlContextMenu ?? new WinControlContextMenu();
                        ret = winControlContextMenu;
                        break;
                    }
                case OT.Tree:
                    {
                        winControlContextMenu = winControlContextMenu ?? new WinControlContextMenu();
                        ret = winControlContextMenu;
                        break;
                    }
                case OT.Tab:
                    {
                        tabContextMenu = tabContextMenu ?? new TabContextMenu();
                        ret = tabContextMenu;
                        break;
                    }
                case OT.WebForms:
                    {
                        webFormsContextMenu = webFormsContextMenu ?? new WebFormsContextMenu();
                        ret = webFormsContextMenu;
                        break;
                    }
                case OT.WebFormSingles:
                    {
                        webFormSinglsContextMenu = webFormSinglsContextMenu ?? new WebFormSinglesContextMenu();
                        ret = webFormSinglsContextMenu;
                        break;
                    }
                case OT.WebFormMasterSlaves:
                    {
                        webFormMasterSlavesContextMenu = webFormMasterSlavesContextMenu ?? new WebFormMasterSlavesContextMenu();
                        ret = webFormMasterSlavesContextMenu;
                        break;
                    }
                case OT.WebFormButton:
                    {
                        webFormButtonContextMenu = webFormButtonContextMenu ?? new WebFormButtonContextMenu();
                        ret = webFormButtonContextMenu;
                        break;
                    }
                case OT.WebFormButtons:
                    {
                        webFormButtonsContextMenu = webFormButtonsContextMenu ?? new WebFormButtonsContextMenu();
                        ret = webFormButtonsContextMenu;
                        break;
                    }
                case OT.WebFormFieldGroups:
                    {
                        webFormFieldGroupsContextMenu = webFormFieldGroupsContextMenu ?? new WebFormFieldGroupsContextMenu();
                        ret = webFormFieldGroupsContextMenu;
                        break;
                    }
                case OT.WebFormFieldGroup:
                    {
                        webFormFieldGroupContextMenu = webFormFieldGroupContextMenu ?? new WebFormFieldGroupContextMenu();
                        ret = webFormFieldGroupContextMenu;
                        break;
                    }
                case OT.WebFormSingle:
                    {
                        webFormSingleContextmenu = webFormSingleContextmenu ?? new WebFormSingleContextMenu();
                        ret = webFormSingleContextmenu;
                        break;
                    }
                case OT.WebFormMasterSlave:
                    {
                        webFormMasterSlaveContextMenu = webFormMasterSlaveContextMenu ?? new WebFormMasterSlaveContextMenu();
                        ret = webFormMasterSlaveContextMenu;
                        break;
                    }
                case OT.WebFormTree:
                    {
                        webFormTreeContextMenu = webFormTreeContextMenu ?? new WebFormTreeContextMenu();
                        ret = webFormTreeContextMenu;
                        break;
                    }
                case OT.WebFormTrees:
                    {
                        webFormTreesContextMenu = webFormTreesContextMenu ?? new WebFormTreesContextMenu();
                        ret = webFormTreesContextMenu;
                        break;
                    }
                case OT.WebFormParameter:
                    {
                        webFormParameterContextMenu = webFormParameterContextMenu ?? new WebFormParameterContextMenu();
                        ret = webFormParameterContextMenu;
                        break;
                    }
                case OT.WebFormParameters:
                    {
                        webFormParametersContextMenu = webFormParametersContextMenu ?? new WebFormParametersContextMenu();
                        ret = webFormParametersContextMenu;
                        break;
                    }
                case OT.WebFormParaByUser:
                    {
                        webFormParaByUserContextMenu = webFormParaByUserContextMenu ?? new WebFormParaByUserContextMenu();
                        ret = webFormParaByUserContextMenu;
                        break;
                    }
                case OT.WebFormParaByUsers:
                    {
                        webFormParaByUsersContextMenu = webFormParaByUsersContextMenu ?? new WebFormParaByUsersContextMenu();
                        ret = webFormParaByUsersContextMenu;
                        break;
                    }
                case OT.WebFormRelation:
                    {
                        webFormRelationContextMenu = webFormRelationContextMenu ?? new WebFormRelationContextMenu();
                        ret = webFormRelationContextMenu;
                        break;
                    }
                case OT.WebFormRelations:
                    {
                        webFormRelationsContextMenu = webFormRelationsContextMenu ?? new WebFormRelationsContextMenu();
                        ret = webFormRelationsContextMenu;
                        break;
                    }
                case OT.WebFormBlank:
                    {
                        webFormBlankContextMenu = webFormBlankContextMenu ?? new WebFormBlankContextMenu();
                        ret = webFormBlankContextMenu;
                        break;
                    }
                case OT.WebFormBlanks:
                    {
                        webFormBlanksContextMenu = webFormBlanksContextMenu ?? new WebFormBlanksContextMenu();
                        ret = webFormBlanksContextMenu;
                        break;
                    }
                default:
                    {
                        //检查节点是否不能删除
                        bool undeleteableNode = false;

                        if (Util.DisallowLabelEditRegEx != string.Empty)
                        {
                            IEnumerator enu = Util.DisAllowLabelEditStrings.GetEnumerator();
                            while (enu.MoveNext())
                            {
                                string str = (string)enu.Current;
                                if (Util.SelectedNode.Text.Equals(str))
                                {
                                    undeleteableNode = true;
                                    break;
                                }
                            }
                        }
                        if (undeleteableNode)
                        {
                            if (baseContext == null)
                            {
                                baseContext = new BaseContextMenu();
                                ret = baseContext;
                            }
                            else
                            {
                                ret = baseContext;
                            }

                        }
                        else
                        {
                            deleteContextMenu = deleteContextMenu ?? new DeleteContextMenu();
                            ret = deleteContextMenu;
                        }

                        break;
                    }
            }
            return ret.getContextMenuStrip();
        }
    }
}